<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/28
 * Time: 18:12
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Base extends Model
{
    /**
     * 排序取最新的
     */
    public function scopeLatest($query)
    {
        return $query->orderBy('updated_at', 'desc')->orderBy('created_at', 'desc');
    }

    /**
     * 取某时间之后的数据
     */
    public function scopeCreatedAfter($query, $time)
    {
        return $query->where('created_at', '>=', $time);
    }

    /**
     * 取某时间之前的数据
     */
    public function scopeCreatedBefore($query, $time)
    {
        return $query->where('created_at', '<=', $time);
    }

    /**
     * 根据ID修改
     */
    public function scopeUpdateById($query, $id, $data)
    {
        return $query->whereId($id)->update($data);
    }

    /**
     * 搜索
     */
    public function scopeSearch($query, $data, $orwhere = null)
    {
        if ($orwhere) {
            return $query->where($data)->orWhere($orwhere);
        } else {
            return $query->where($data);
        }
    }

    /*
     * 取多条数据ids
     */
    public function scopeIds($query, $ids, $not = false)
    {
        if ($not) {
            return $query->whereIn('id', $ids);
        } else {
            return $query->whereNotIn('id', $ids);
        }
    }

    /*
     * 过滤软关闭了的数据
     */
    public function scopeSwitch($query, $status = 1)
    {
        return $query->where('switch', $status);
    }


    /**
     * 过滤支付失败的数据
     */
    public function scopeStatus($query, $status = 1)
    {
        return $query->where('status', $status);
    }

    public function scopeType($query, $type)
    {
        return $query->where('type', $type);
    }


    /**
     * 过滤支付失败的数据
     */
    public function scopeOpenid($query, $openid)
    {
        return $query->where('openid', $openid);
    }


    /**
     * 过滤支付失败的数据
     */
    public function scopeMember($query, $memberId)
    {
        return $query->where('member_id', $memberId);
    }
}